package com.hlkj.baojin.utils.hik;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class LocalMac {
	/**
	 * 
	 * @author weixin6 2015-9-16 下午3:35:56
	 * @return 本地IP
	 * @modificationHistory=========================逻辑或功能性重大变更记录
	 * @modify by user: {修改人} 2015-9-16
	 * @modify by reason:{原因}
	 */
	public static InetAddress localIP(){
		InetAddress ip=null;
		try {
			ip=InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			System.out.println("UnknownHostException 获取本地IP地址");
		}
		return ip;
	}
	/**
	 * 
	 * @author weixin6 2015-9-16 下午3:36:12
	 * @return 本地Mac地址
	 * @modificationHistory=========================逻辑或功能性重大变更记录
	 * @modify by user: {修改人} 2015-9-16
	 * @modify by reason:{原因}
	 */
	public static String getLocalMac(){
		
		//本地IP
		InetAddress ip=localIP();
		if(ip==null){
			return "00-00-00-00-00-00";
		}
		StringBuffer sb = new StringBuffer("");
		try {
			byte[]  mac = NetworkInterface.getByInetAddress(ip).getHardwareAddress();
			//System.out.println("mac数组长度："+mac.length);
			
			for(int i=0; i<mac.length; i++) {
				if(i!=0) {
					sb.append("-");
				}
				//字节转换为整数
				int temp = mac[i]&0xff;
				String str = Integer.toHexString(temp);
				//System.out.println("每8位:"+str);
				if(str.length()==1) {
					sb.append("0"+str);
				}else {
					sb.append(str);
				}
			}
		} catch (SocketException e) {
			System.out.println("SocketException: 获取mac地址");
		}
		
		return sb.toString().toUpperCase();
	}
}
